package com.yu.smsinterceptor;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yu.smsinterceptor.adapter.InterceptPhoneListAdapter;
import com.yu.smsinterceptor.mode.Phone;
import com.yu.smsinterceptor.service.PhoneService;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * 编辑拦截号码的界面
 */
@ContentView(R.layout.activity_intercept)
public class InterceptPhoneActivity extends AppCompatActivity {

    //Toolbar
    @ViewInject(R.id.toolbar)
    private Toolbar mToolbar;
    //填写拦截号码的编辑框
    @ViewInject(R.id.edit_phone_number)
    private EditText edit_phoneNumber;
    //拦截的号码列表
    @ViewInject(R.id.recycler_intercept_phone_list)
    private RecyclerView mRecyclerView;
    private InterceptPhoneListAdapter mAdapter;

    private PhoneService mService = new PhoneService();
    //拦截的号码集合
    private List<Phone> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new InterceptPhoneListAdapter(this, data);
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 添加一个拦截号码
     *
     * @param v
     */
    @Event(R.id.btn_add_phone_number)
    private void addInterceptPhone(View v) {
        Long number = Long.valueOf(edit_phoneNumber.getText().toString());
        Phone phone = new Phone();
        phone.setPhoneNumber(number);
        mService.save(phone);
        Snackbar.make(v, "添加成功", Snackbar.LENGTH_SHORT).show();
        updateUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUI();
    }

    /**
     * 更新界面
     */
    public void updateUI() {
        new RefreshThread().start();
    }

    //刷新
    private static final int REFRESH = 1;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH:
                    //接收到刷新指令后刷新界面
                    data = (List<Phone>) msg.obj;
                    mAdapter.refresh(data);
                    break;
            }

        }
    };

    /**
     * 刷新线程
     */
    class RefreshThread extends Thread {

        @Override
        public void run() {
            super.run();
            List<Phone> list = mService.list();
            android.os.Message msg = new android.os.Message();
            msg.what = REFRESH;
            msg.obj = list;
            mHandler.sendMessage(msg);
        }
    }
}
